//
//  JZBLoginViewController.m
//  JZB
//
//  Created by  on 2022/8/29.
//

#import "JZBLoginViewController.h"
#import "JZBAddCountViewController.h"

@interface JZBLoginViewController ()
@property (strong, nonatomic) IBOutlet JZBTextField *input1;
@property (strong, nonatomic) IBOutlet JZBTextField *input2;

@end

@implementation JZBLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//}
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//}
- (void)initUI{
    self.title = LS(@"登录");
    self.input2.secureTextEntry = YES;
    self.input1.attributedPlaceholder = [[NSAttributedString alloc] initWithString:LS(@"请输入账号") attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}];
    self.input2.attributedPlaceholder = [[NSAttributedString alloc] initWithString:LS(@"请输入密码") attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]}];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    btn.tag = 204;
    [btn setImage:[UIImage imageNamed:@"查看"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(acionforbtn:) forControlEvents:UIControlEventTouchUpInside];
    self.input2.rightViewMode = UITextFieldViewModeAlways;
    self.input2.rightView = btn;
}
- (IBAction)acionforbtn:(UIButton *)sender {
    switch (sender.tag) {
        case 201:///登录
        {
            if (self.input1.text.length <= 0) {
                return [RLHud msg:LS(@"账号不能为空")];
            }else if (self.input2.text.length <= 0){
                return [RLHud msg:LS(@"密码不能为空")];
            }else if ([self.input1.text isNumberOrAlphabet] == NO){
                return [RLHud msg:LS(@"账号只能包含数字和字母")];
            }else if ([self.input2.text isNumberOrAlphabet] == NO){
                return [RLHud msg:LS(@"密码只能包含数字和字母")];
            }
            [CCHttp post:api_login para:@{@"userName":self.input1.text,@"password":self.input2.text} success:^(id  _Nonnull response) {
               NSString *token = [response valueForKey:@"data"];
                [CCHttp http].header = @{@"Authorization":token};
                [CCHttp get:api_userinfo para:nil header:nil success:^(id  _Nonnull response) {
                    JZBUser *user = [JZBUser mj_objectWithKeyValues:[response valueForKey:@"data"]];
                    user.token = token;
                    APP.user = user;
                    [JZBUser bg_clear:nil];
                    [APP.user bg_saveOrUpdate];
                    [[NSNotificationCenter defaultCenter] postNotificationName:notifyUserUpdate object:nil];
                    [self backBtnAction];
                } fail:^(NSError * _Nonnull error) { }];
            } fail:^(NSError * _Nonnull error) { }];
        }
            break;
        case 202:{///忘记密码
            JZBAddCountViewController *vc = [JZBAddCountViewController new];
            vc.type = AddCountTypePWD;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 203:{///注册用户
            JZBAddCountViewController *vc = [JZBAddCountViewController new];
            vc.type = AddCountTypeNew;
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 204:///查看密码
            self.input2.secureTextEntry = !self.input2.secureTextEntry;
            break;
        default:
            break;
    }
}

@end
